package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.*;
import com.zhentao.domain.vo.Result;
import com.zhentao.mapper.SysUserSiteMapper;
import com.zhentao.service.SysSiteService;
import com.zhentao.service.SysUserSiteService;
import com.zhentao.utils.JwtService;
import com.zhentao.utils.UserUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author 86133
* @description 针对表【sys_user_site】的数据库操作Service实现
* @createDate 2024-08-20 17:17:51
*/
@Service
public class SysUserSiteServiceImpl extends ServiceImpl<SysUserSiteMapper, SysUserSite>
    implements SysUserSiteService{
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private SysSiteService sysSiteService;
    @Override
    public Result collectSite( Integer sid) {
        int id = userUtils.GetUserId();

        //不能重复收藏
        SysUserSite one = this.getOne(Wrappers.<SysUserSite>lambdaQuery().eq(SysUserSite::getSid, sid).eq(SysUserSite::getUid, id));
        if (one != null){
            return Result.build(null,400,"不能重复收藏");
        }
        SysUserSite sysUserSite = new SysUserSite();
        sysUserSite.setSid(sid);
        sysUserSite.setUid(id);
        sysUserSite.setCreateTime(new Date());
        boolean save = this.save(sysUserSite);
        return save?Result.build(null,200,"收藏成功"):Result.build(null,400,"收藏失败");
    }

    @Override
    public Result getUserList() {
        String chinese = stringRedisTemplate.opsForValue().get("chinese");
        List<Province> provinces = JSON.parseArray(chinese, Province.class);
        int id = userUtils.GetUserId();
        List<SysUserSite> sysUserSites = this.list(Wrappers.<SysUserSite>lambdaQuery().eq(SysUserSite::getUid, id));
        for (SysUserSite sysUserSite : sysUserSites) {
            if (StringUtils.checkValNotNull(sysUserSite.getSid())) {
                SysSite sysSite = sysSiteService.getOne(Wrappers.<SysSite>lambdaQuery().eq(SysSite::getId, sysUserSite.getSid()));
                Integer provinceId = sysSite.getProvinceId();
                Integer cityId = sysSite.getCityId();
                Integer areaId = sysSite.getAreaId();
                if (StringUtils.checkValNotNull(provinceId)) {
                    for (Province province : provinces) {
                        if (Objects.equals(province.getId(), provinceId)) {
                            sysSite.setProvinceName(province.getName());
                            System.out.println(province.getCityList());
                            for (City city : province.getCityList()) {
                                if(Objects.equals(city.getId(), cityId)){
                                    sysSite.setCityName(city.getName());
                                    for (Area area : city.getAreaList()) {
                                        if(Objects.equals(area.getId(), areaId)){
                                            sysSite.setAreaName(area.getName());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                sysUserSite.setSysSite(sysSite);
            }
        }
        return Result.build(sysUserSites,200,"获取成功");
    }


}




